package org.bugjail.integration.mantis;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.bugjail.data.TestCase;
import org.bugjail.data.TestCaseExecution;
import org.bugjail.dataaccess.DAOFactory;
import org.bugjail.dataaccess.objects.TestCaseExecutionDAO;
import org.bugjail.integration.mantis.data.CR;
import org.bugjail.integration.mantis.dataaccess.objects.RepositoryCR;

public class Mantis {
	public List<CR> getCRsForTestCase(TestCase tc) {
		List<CR> crs = new LinkedList<CR>();
		try {
			TestCaseExecutionDAO dao = DAOFactory.getDAOFactory().getTestCaseExecutionDAO();
			
			RepositoryCR rcr = new RepositoryCR();
			
			List<TestCaseExecution> executions = dao.retrieveExecutionsOfTestCase(tc);
			Set<Integer> ids = new HashSet<Integer>();
			for (TestCaseExecution tce : executions) {
				String[] crgroup =  tce.getCrsRelated().split(",");
				for (String string : crgroup) {
					try {
						ids.add(Integer.parseInt(string));
					} catch (Throwable t) {
					}
				}
			}
			for (Integer id : ids) {
				CR crToPut = rcr.getCRById(id);
				if (crToPut != null) {
					crs.add(crToPut);
				}
			}
		} catch (Exception e) {

		}
		return crs;
	}
}
